class Log < ActiveRecord::Base
  belongs_to :loggable, :polymorphic => true
  has_many :log_details, :dependent => :destroy
  
  # NOTE: logs belong to a user
  belongs_to :user
  belongs_to :owner, class_name: 'User'
  
  def add_detail label, detail
    return false unless label
    log_details << LogDetail.new(:label => label, :detail => detail.to_s)
  end
  
  
  # submit as details= {
  #  "Success" => false,
  #  "Transaction ID" => 2,
  #  "Message" => "Lorem ipsum dolor sit amet, consectetur adipisicing elit laborum."
  # }
  #
  def details= details
    return false unless details.is_a? Hash
    details.each do |k, v|
      return false unless add_detail k, v
    end
  end
  
  def details
    log_details
  end

  def user_name
    user.present? ? user.name : 'System'
  end

end
